/**
 * 
 */
package com.googlecode.lohr.parser.combinators.matcher;


import com.googlecode.lohr.parser.combinators.MatchError;
import com.googlecode.lohr.parser.combinators.node.StringNode;

public final class StringMatcher extends Matcher
{
	private final String _target;

	public StringMatcher(String target)
	{
		_target= target;
	}

	@Override
	public void match(MatchContext ctx, int start) 
	{
			String input= ctx.getInput();
			int i= 0;
			int l= _target.length();
			int e= input.length();
			while (i < l && (start+i) < e && input.charAt(start + i) == _target.charAt(i))
				i++;
			if (i == l) {
				ctx.matchFound(this, start, new StringNode(this, start, start + l, _target));
			}
			else
				ctx.matchError(this, start, new MatchError("Expected '"+_target+"'", start + i));
			
			ctx.matchComplete(this, start);
	}
	
	public String getTarget() {
		return _target;
	}
}